//
// Created by jerry on 2024/9/12.
//

#ifndef DATASTRUCTURE_TXTREADWRITEHELPER_H
#define DATASTRUCTURE_TXTREADWRITEHELPER_H
#include <iostream>
#include <fstream>
class WriteAndRead{
public:
    template <class Input>
    explicit WriteAndRead(Input&& filename):_filename(std::forward<Input>(filename))
    {}
    template <typename T>
    void write(const T& val){
        std::ofstream ofs(_filename);
        ofs << val << std::endl;
        ofs.close();
    }//这里需要你重载一个operator<<T

private:
    std::string _filename;
};
struct Date{
    int _year = 0;
    int _month = 0;
    int _day = 0;
    Date(int year,int month,int day):_year(year),_month(month),_day(day)
    {}
};
std::ostream& operator<<(std::ostream& out,const Date& d){
    out << d._year << " " << d._month << " " << d._day << std::endl;
}
struct BookInfor{
    std::string _name;
    std::string _writer;
    std::string _id;
    template <typename Input>
    explicit BookInfor(Input&& name,Input&& writer,Input&& id):_name(std::forward<Input>(name)),
    _writer(std::forward<Input>(writer)),_id(std::forward<Input>(id))
    {}

};
std::ostream& operator<<(std::ostream& out,const BookInfor& d){
    out << d._name << " " << d._writer << " " << d._id << std::endl;
}
void Writetest(){
//    WriteAndRead war("C:\\Users\\herui\\Desktop\\test.txt.txt");
//    war.write(Date(1,2,3));
    BookInfor ret(std::string("hello"),std::string("world"),std::string("hehe"));
    WriteAndRead war("C:\\Users\\herui\\Desktop\\test.txt.txt");
    war.write(ret);
}
#endif //DATASTRUCTURE_TXTREADWRITEHELPER_H
